package com.operate.service;

import com.operate.bean.VO.*;
import com.operate.dao.ReservationMapper;
import com.operate.dao.ScreenDataMapper;
import com.operate.dao.WaitMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZZZ
 * @time: 2023/11/5 22:35
 */
@Slf4j
@Service
public class CommonNumService {
    @Autowired
    private ScreenDataMapper screenMapper;
    @Autowired
    private WaitMapper waitMapper;
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private WaitService waitService;
    @Autowired
    private ReservationService reservationService;

    /**
     * @name: activeNumList
     * @description: 商家活跃数统计
     * @auther: ZeckZheng
     */
    public List<CommonVO> activeNumList() {
        List<CommonVO> list = screenMapper.activeNumList();
        for (CommonVO commonVO : list) {
            commonVO.setMark("活跃数统计");
        }
        return list;
    }

    /**
     * @name: orderNum
     * @description: 订单数统计（online/等位/预约）
     * @auther: ZeckZheng
     */
    public List<OrderNumVO> orderNum() {
        List<OrderNumVO> list = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM");
        for (int i = 11; i >= 0; i--) {
            String statTime = localDate.minusMonths(i).format(dtf);
            OrderNumVO vo = new OrderNumVO();
            vo.setStatTime(statTime);
            vo.setOnlineNum(0L);
            vo.setOnlineTotal(0L);
            vo.setReservationNum(0L);
            vo.setReservationTotal(0L);
            vo.setWaitNum(0L);
            vo.setWaitTotal(0L);
            list.add(vo);
        }

        //Map<String, Map<String, Object>> onlineMap = screenMapper.onlineNum();
        //等位数统计（近12个月）
        Map<String, Map<String, Object>> waitMap = screenMapper.waitNum();
        //预约数统计（近12个月）
        Map<String, Map<String, Object>> reservationMap = screenMapper.reservationNum();

        //Long onlineTotal = (Long) onlineMap.get("BF").get("statNum");
        Long waitTotal = (Long) waitMap.get("BF").get("statNum");
        Long reservationTotal = (Long) reservationMap.get("BF").get("statNum");

        for (OrderNumVO vo : list) {
            //vo.setOnlineTotal(onlineTotal);
            vo.setWaitTotal(waitTotal);
            vo.setReservationTotal(reservationTotal);
            //if (onlineMap.get(vo.getStatTime()) != null) {
            //vo.setOnlineNum((Long) onlineMap.get(vo.getStatTime()).get("statNum"));
            //}

            if (waitMap.get(vo.getStatTime()) != null) {
                vo.setWaitNum((Long) waitMap.get(vo.getStatTime()).get("statNum"));
            }

            if (reservationMap.get(vo.getStatTime()) != null) {
                vo.setReservationNum((Long) reservationMap.get(vo.getStatTime()).get("statNum"));
            }

        }

        for (int i = 0; i < 12; i++) {
            //Long onlineNum = 0L;
            Long waitNum = 0L;
            Long reservationNum = 0L;
            for (int j = 0; j <= i; j++) {
                //onlineNum = onlineNum + list.get(j).getOnlineNum();
                waitNum = waitNum + list.get(j).getWaitNum();
                reservationNum = reservationNum + list.get(j).getReservationNum();

            }
            OrderNumVO vo = list.get(i);
            //vo.setOnlineTotal(onlineNum + vo.getOnlineTotal());
            vo.setWaitTotal(waitNum + vo.getWaitTotal());
            vo.setReservationTotal(reservationNum + vo.getReservationTotal());
        }
        return list;

    }

    /**
     * @name: commonData
     * @description: 基础数据统计
     * @auther: ZeckZheng
     */
    public List<CommonVO> commonData() {
        List<CommonVO> list = new ArrayList<>();
        //商家新增数（今日、本周、总）
        list.addAll(screenMapper.incrCustomNumList());
        //今日 近30日 排队数
        list.addAll(screenMapper.incrWaitNumList());
        //今日 近30日 预约数
        list.addAll(screenMapper.incrReservationNumList());

        //list.addAll(screenMapper.incrOnlineNumList());

        //今日 等位渠道排队数统计
        list.addAll(screenMapper.todayWaitTypeNumList());
        //今日 预约渠道排队数统计
        list.addAll(screenMapper.todayReservationTypeNumList());
        //近30天 预约时间段统计
        list.addAll(reservationService.reservationTimeStat());
        //商家活跃数统计 日 周 月
        list.addAll(activeNumList());

        return list;
    }

    /**
     * @name: wrDayTypeNum
     * @description: 等位/预约 渠道数量统计（近30日）
     * @auther: ZeckZheng
     */
    public List<WRDayTypeNumVO> wrDayTypeNum() {
        List<WRDayTypeNumVO> list = new ArrayList<>();
        list.addAll(waitMapper.waitDayTypeList());
        list.addAll(reservationMapper.reservationDayTypeList());
        return list;
    }

    /**
     * @name: wrMonthList
     * @description: 等位/预约 渠道数量统计（近12个月）
     * @auther: ZeckZheng
     */
    public List<WRMonthNumVO> wrMonthList() {
        List<WRMonthNumVO> list = new ArrayList<>();
        list.addAll(waitService.waitMonthList());
        list.addAll(reservationService.reservationMonthList());
        return list;
    }

    /**
     * @name: dinersNumStatList
     * @description: 就餐人数规模统计
     * @auther: ZeckZheng
     */
    public List<DinersNumStatVO> dinersNumStatList() {
        List<DinersNumStatVO> list = new ArrayList<>();
        list.addAll(waitService.dinersNumStatList());
        list.addAll(reservationService.dinersNumStatList());
        return list;
    }

}
